//注册页面
#include "erollwindow.h"
#include "ui_erollwindow.h"
#include<QMessageBox>
#include<string>
#include <QRegularExpressionValidator>
#include"clientSock.h"
#include"MD5.h"
#include"recivedata.h"
#include<QObject>


erollWindow::erollWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::erollWindow)
{
    ui->setupUi(this);
    QRegularExpressionValidator* validator = new QRegularExpressionValidator(
        QRegularExpression("^[0-9]+$"), this);
    ui->Phone->setValidator(validator);

    QObject::connect(&ReceiveData::getInstance(),&ReceiveData::RegistSignal,this,&erollWindow::plotDialogregist,Qt::QueuedConnection);
}

erollWindow::~erollWindow()
{
    delete ui;
}

void erollWindow::on_eroll_clicked()
{
    std::string phone = ui->Phone->text().toStdString();
    std::string username = ui->Username->text().toStdString();
    QString str1pass = ui->firstpass->text();
    QString str2pass = ui->secondpass->text();
    if(str1pass != str2pass){
        QMessageBox::information(this,"提示","密码不匹配");
        ui->firstpass->clear();
        ui->secondpass->clear();
        return;
    }
    std::string pass = md5Encrypt(str1pass).toStdString();
    std::string email = ui->email->text().toStdString();

    if(phone.size()==0||pass.size()==0||username.size()==0){
        QMessageBox::information(this,"提示","信息不全");
    }
    else{
        //头
//        Client::getInstance().connectSever();
        CloudDrive::Packet pkt;
        pkt.header.type = CloudDrive::MessageType::REGISTER;
        pkt.header.version = CloudDrive::PROTOCOL_VERSION;
        pkt.header.timestamp = CloudDrive::now_millis();
        // 文件信息
        CloudDrive::UserInfo uIfo;
        strncpy(uIfo.account,phone.c_str(),CloudDrive::MAX_ACCOUNT_LEN - 1);
        strncpy(uIfo.userName,username.c_str(),CloudDrive::MAX_ACCOUNT_LEN - 1);
        strncpy(uIfo.password,pass.c_str(),CloudDrive::MAX_ACCOUNT_LEN - 1);
        strncpy(uIfo.email,email.c_str(),CloudDrive::MAX_ACCOUNT_LEN - 1);
        Client::getInstance().sendData(CloudDrive::makeUserPacket(pkt,uIfo,0));
    }

}

void erollWindow::plotDialogregist(CloudDrive::MessageReturn msg)
{
    switch (msg) {
    case CloudDrive::MessageReturn::REGISTER_OK:{
        QMessageBox::information(this,"注册","注册成功");
        break;
    }
    case CloudDrive::MessageReturn::REGISTER_EXIST:{
        QMessageBox::information(this,"注册","用户存在");
        break;
    }
    case CloudDrive::MessageReturn::REGISTER_NO:{
        QMessageBox::information(this,"注册","注册失败");
        break;
    }
    default:
        break;
    }
}
